
from django.contrib.auth.models import Group
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver

from main.apps.op_log.utils import oplog_utils


@receiver(signal=[post_save, pre_save], sender=Group)
def save_group_log(sender, instance, **kwargs):
    if instance._state.adding:
        return False
    object_type = getattr(instance, 'object_type', 'group')

    if kwargs.get('created'):
        log = oplog_utils.model_instance_diff(None, instance)
        oplog_utils.save_op_log(instance, object_type, log, '创建用户组')
    else:
        log = oplog_utils.model_instance_changes(instance)
        oplog_utils.save_op_log(instance, object_type, log, '修改用户组')
